package com.shop.mapper;

import com.shop.dto.CarItem;
import com.shop.model.Cart;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CartMapper {
    // 查看所有数据
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "goodId", column = "good_id"),
            @Result(property = "goods.id", column = "good_id"),
            @Result(property = "goods.name", column = "good_name"),
            @Result(property = "goods.image", column = "good_image")
    })
    @Select("SELECT  c.id,c.user_id, c.good_id, c.count, c.price, " +
            "g.name as good_name, g.image as good_image " +
            "FROM cart c JOIN goods g ON c.good_id = g.id " +
            "WHERE c.user_id = #{userId}")
    List<Cart> selectCartsByUserId(Long userId);

    @Select("SELECT count FROM cart WHERE good_id = #{goodId}")
    int getCountByGoodId(Long goodId);

    @Update("UPDATE cart SET count = count - 1, price = price - (SELECT sale_price FROM goods WHERE id = #{goodId}) WHERE good_id = #{goodId}")
    int decreaseQuantity(Long goodId);

    @Delete("DELETE FROM cart WHERE good_id = #{goodId}")
    int deleteCartByGoodId(Long goodId);

    @Delete("DELETE FROM cart WHERE user_id = #{userId}")
    int deleteAllByUserId(Long userId);

    @Select("SELECT stock FROM goods WHERE id = #{goodId}")
    int getStockByGoodId(Long goodId);

    @Update("UPDATE cart SET count = count + 1, price = price + (SELECT sale_price FROM goods WHERE id = #{goodId}) WHERE good_id = #{goodId}")
    int increaseQuantity(Long goodId);

    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "goodId", column = "good_id"),
            @Result(property = "goods.id", column = "good_id"),
            @Result(property = "goods.name", column = "good_name"),
            @Result(property = "goods.image", column = "good_image")
    })
    @Select("SELECT c.id,c.user_id, c.good_id, c.count, c.price, " +
            "g.name as good_name, g.image as good_image " +
            "FROM cart c JOIN goods g ON c.good_id = g.id " +
            "WHERE c.good_id = #{goodId}")
    Cart getCartByGoodId(Long goodId);

    @Update("UPDATE goods SET stock = stock - #{count} WHERE id = #{goodId}")
    int updateStock(@Param("goodId") Long goodId, @Param("count") int count);

    @Delete("DELETE FROM cart WHERE id = #{cartId}")
    int deleteCartById(Long cartId);

    @ResultType(CarItem.class)
    @Select("SELECT cart.id as id, good_id as goodid, cart.count as num, cart.user_id as userid, " +
            "goods.name as goodname, goods.image as goodimage, " +
            "goods.sale_price * cart.count as goodprice " +
            "FROM cart LEFT OUTER JOIN goods ON cart.good_id = goods.id " +
            "WHERE cart.user_id = #{id}")
    List<CarItem> selectCartItemsByUserId(Long id);

    @Insert("INSERT INTO cart (user_id, good_id, count, price) VALUES (#{userId}, #{goodId}, #{count}, #{price})")
    void addCart(Cart cart);

    // 新增：根据用户ID和商品ID查询购物车项
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "goodId", column = "good_id"),
            @Result(property = "goods.id", column = "good_id"),
            @Result(property = "goods.name", column = "good_name"),
            @Result(property = "goods.image", column = "good_image")
    })
    @Select("SELECT c.id, c.user_id, c.good_id, c.count, c.price, " +
            "g.name as good_name, g.image as good_image " +
            "FROM cart c JOIN goods g ON c.good_id = g.id " +
            "WHERE c.user_id = #{userId} AND c.good_id = #{goodId}")
    Cart getCartByUserIdAndGoodId(@Param("userId") Long userId, @Param("goodId") Long goodId);

    // 新增：更新购物车项
    @Update("UPDATE cart SET count = #{count}, price = #{price} WHERE user_id = #{userId} AND good_id = #{goodId}")
    void updateCart(Cart cart);

    //    插入CartItem信息
    @Insert("INSERT INTO cart_item (id, goodid, num, userid,goodname, goodimage,goodprice) VALUES (#{id}, #{goodid}, #{num}, #{userid},#{goodname},#{goodimage},#{goodprice})")
    int addCartItem(CarItem cart);
    @Select("SELECT cart.id as id, good_id as goodid, cart.count as num, cart.user_id as userid, " +
            "goods.name as goodname, goods.image as goodimage, " +
            "goods.sale_price * cart.count as goodprice " +
            "FROM cart LEFT OUTER JOIN goods ON cart.good_id = goods.id " +
            "WHERE cart.id = #{id}")
    CarItem getCartItem(Integer id);
    @Select("select *from cart where id=#{id}")
    Cart getTotalPrice(Integer id);
    @Delete("DELETE FROM cart WHERE id = #{id}")
    int deleteCartbyId(Integer id);

}